import { observable, action, runInAction } from 'mobx';
import { Http } from '../services/Http';
import * as lodash from 'lodash'
import { pagedDatasAsync, userInfo } from '../basicData/api'

namespace common {
  export const http: Http = new Http(); 
}

export class AppStore {
  @observable public isLoading: boolean;
  @observable public editModalVisible: boolean;

  @observable public selectedRowKeys: string[];
  @observable public selectedRowIds: string[];
  @observable public selectedRows: any[];
  @observable public selectedRowsBindIds: any[];

  @observable public currentPage: number;
  @observable public sorting: string;
  @observable public filter: any;
  
  public constructor() {
    this.isLoading = false;
    this.editModalVisible = false
    this.selectedRowKeys = []
    this.selectedRowIds = []
    this.selectedRows = []
    this.currentPage = 1
    this.sorting = ''
    this.filter = {}
  }

  @action public loading = (isLoad: boolean) => {
    this.isLoading = isLoad
  }
  
  @action public onSelectChange = (selectedRowKeys: string[], selectedRows: any) => {
    this.selectedRowKeys = selectedRowKeys
    this.selectedRows = selectedRows
    let arrId: string[] = [];
    let bindIds: string[] = [];

    selectedRows.forEach((element: any) => {
      arrId.push(element.id);
      if(element.authObject){
        element.authObject.map((item: any) => {
          bindIds.push(item.id);
        })
      } else if(element.roles) {
        element.roles.map((item: any) => {
          bindIds.push(item.id)
        })
      }
    });
    this.selectedRowsBindIds = lodash.uniq(bindIds)
    this.selectedRowIds = arrId
  }

  @action public pageChange = (page: number) => {
    this.currentPage = page
  } 

  @action public showEditModal = (visible: boolean) => {
    this.editModalVisible = visible
  }
}